﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using SAKS.Models;

namespace SAKS.ConfigManager
{
    public static class MembershipConfig
    {
        private static NameValueCollection settings;
        private static int defaultMaxPasswordLength = 6;
        private static int defaultMinPasswordLength = 30;
        private static PasswordFormat defaultPasswordFormat = PasswordFormat.Clear;
        private static int maxPasswordLength;
        private static int minPasswordLength;
        private static PasswordFormat passwordFormat;


        static MembershipConfig()
        {
            settings = (NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection(@"Services/Membership");
            if (settings == null)
                throw new ConfigurationErrorsException("Services/Membership section doesn't exist");
            ConfigMaxPasswordLength();
            ConfigMinPasswordLength();
            ConfigPasswordEncryptType();
        }

        private static void ConfigPasswordEncryptType()
        {
            var temp = settings["PasswordEncryptType"];
            if (temp == null)
                passwordFormat = defaultPasswordFormat;
            else
                passwordFormat = (PasswordFormat)Enum.Parse(typeof(PasswordFormat),temp);
        }

        private static void ConfigMinPasswordLength()
        {
            var temp = settings["MinPasswordLength"];
            if (temp == null)
                minPasswordLength = defaultMinPasswordLength;
            else
                minPasswordLength = int.Parse(temp);
        }

        private static void ConfigMaxPasswordLength()
        {
            var temp = settings["MaxPasswordLength"];
            if (temp == null)
                maxPasswordLength = defaultMaxPasswordLength;
            else
                maxPasswordLength = int.Parse(temp);
        }

        public static PasswordFormat PassworFormat
        {
            get
            {
                return passwordFormat;
            }
        }

        public static int MaxPasswordLength 
        {
            get
            {
                return maxPasswordLength;
            } 
        }

        public static int MinPasswordLength
        {
            get
            {
                return minPasswordLength;
            }
        }
    }
}
